<%
' +----------------------------------------------------------------------
' | POPASP [ ASP MVC ]
' +----------------------------------------------------------------------
' | Copyright (c) 2016 http://popasp.com All rights reserved.
' +----------------------------------------------------------------------
' | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
' +----------------------------------------------------------------------
' | Author: popasp <1737025626@qq.com>
' +----------------------------------------------------------------------
Class POPASP_PAGETRACE
	public options
	
	function handlerLink( action,param,title,content )
		dim href
		href = U_( array(C_("SYSTEM_MODULE") & "/" & action, param )  )
		handlerLink = C_("TRACE_HANDLER_LINK")
		handlerLink = replace( handlerLink,"HREF",href )
		handlerLink = replace( handlerLink,"TITLE",title )
		handlerLink = replace( handlerLink,"CONTENT",content )		
	end function
	
	'删除文件
	function delete( action , param , title )		
		delete = handlerLink( action,param,title,C_("TRACE_DELETE_SHOW") )
	end function
	
	'删除所有文件
	function deleteAll( action , param , title )		
		deleteAll = handlerLink( action,param,title,C_("TRACE_DELETE_ALL_SHOW") )
	end function
	
	function help( href,title )
		if title = "" Then
			title = "显示帮助"
		end if
		help = C_("TRACE_HANDLER_LINK")
		help = replace( help,"HREF", href )
		help = replace( help,"TITLE", title )
		help = " " & replace( help,"CONTENT",C_("TRACE_HELP_SHOW") )
	end function
	
	'POPASP_类的帮助
	function classHelp( byref config ,byref className )
		classHelp = C_("TRACE_HANDLER_LINK")
		classHelp = replace( classHelp,"HREF",C_(config) )
		classHelp = replace( classHelp,"TITLE", className )
		classHelp = " " & replace( classHelp,"CONTENT",className ) & " "	
	end function
	
	'读取文件
	function readFile( file , title )		
		readFile = " " & handlerLink( "readFile","file=" & file ,title,C_("TRACE_READ_SHOW") )
	end function
	
	'读取popasp_class结构
	function readPOPASPClass( sClass  )		
		readPOPASPClass = " " & handlerLink( "readPOPASPClass","class=" & sClass ,sClass," " & lcase(sClass) & " " )
	end function
	
	'删除解析文件
	function removeCompileFile( c,a )
		removeCompileFile = " " &  delete( "removeCompileFile" , "controller=" & c & "&action=" & a , "删除解析文件" )
	end function
	
	'删除文件
	function removeFile( file ,title )		
		removeFile = " " &  delete( "removeFile" , "file=" & file , title )
	end function
	
	'删除文件夹
	function removeFolder( folder ,title )		
		removeFolder = " " &  deleteAll( "removeFolder" , "folder=" & folder , title )
	end function
	
	'删除session
	function removeSession( sess_key , title )
		removeSession = " " & delete( "removeSession" , "key=" & sess_key,title )
	end function
	
	'读取session
	function readSession( sess_key , title )		
		readSession = " " & handlerLink( "readSession","key=" & sess_key ,title,C_("TRACE_READ_SHOW") )
	end function
	
	function clearCompileDir(  )
		clearCompileDir = " " &  deleteAll( "clearCompileDir" , "", "删除所有解析文件" )
	end function
	
	sub show()		
		on error resume next
		dim template,dir,tpl,base,temp,vars,key,sql,arr,keys,errors,i,sites,item,matches,filePath,dict
		
		set template = P_("POPASP_TEMPLATE")
		set template.tpl_vars = nothing
		set template.tpl_vars = D_
		template.caching = -1
		template.layout_on = False
		template.left_delimiter = "{"
		template.right_delimiter = "}"
		template.use_ctrl_tpl = False
		template.tpl_suffix = ".html"
		dir = POP_MVC.mvc_dir & "Tpl/"
		tpl = "page_trace"
		template.template_dir = dir
		
		temp = REQUEST_TIME__ & " " & Request.ServerVariables("SERVER_PROTOCOL") & " " &  Request.ServerVariables("REQUEST_METHOD") & " : " & Request.ServerVariables("SCRIPT_NAME")
		if Request.ServerVariables("QUERY_STRING") <> "" Then
			temp = temp & "?" & Request.ServerVariables("QUERY_STRING")
		end if
		
		POP_MVC.dict.push base,"请求信息",temp
		
		' 添加运行时间
		POP_MVC.dict.push base,"运行时间",G_( array("beginTime","viewEndTime") ) & "ms ( Load:" & G_( array("beginTime","loadTime") ) & "ms Init:" & G_( array("loadTime","initTime") ) & "ms Exec:" & G_( array("initTime","viewStartTime") ) & "ms Template:"  & G_( array("viewStartTime", "viewEndTime") ) & "ms ) "  & " | Focus " & "( Server.Execute : " & G_( array("ExecuteStartTime", "ExecuteEndTime") ) & "ms "   & " compile : " & G_( array("compileStartTime", "compileEndTime") ) & "ms " & " test:" & G_( array("testStartTime", "testEndTime") ) & "ms " & " )"  
		
		' 添加使用到的类
		set dict = POP_MVC.Arr.toDict(POP_MVC.dClass.keys)
		set dict = POP_MVC.Dict.flip( dict )
		keys = Array( "STRING","ARR","DICTIONARY","EXCEL","SQLITE3","PAGE" )
		
		var_Export 
		
		for each key in dict
			temp = POP_MVC.ltrim(key,"POPASP_")
			if POP_MVC.Arr.Exists( keys, temp ) then
				dict(key) = classHelp( "HELP_" & temp,temp)
			else				
				if temp <> key then
					dict(key) = readPOPASPClass( temp )
				else
					dict(key) = key
				end if
			end if
		next
		
		' popasp_mvc类需要手动添加
		POP_MVC.dict.push base,"popasp_", readPOPASPClass("mvc") & " , " & Replace(join(dict.items,","),"POPASP_","") & " (" & (POP_MVC.dClass.Count + 1) & "个)"
		
		' 数据库读取次数
		temp = ""
		temp = N_("db_query")
		if isEmpty( temp ) Then
			temp = 0
		end if
		temp = temp & " queries "
		
		' 数据库写入次数
		if isEmpty( N_("db_write") ) Then
			temp = temp &  0 & " writes"
		else
			temp = temp & N_("db_write") & " writes"			
		end if
		
		POP_MVC.dict.push base,"查询信息", temp
		
		
		temp = POP_MVC.dDict.count & " Dictionaries " ' 用到的 Dictionaries 个数
		if isEmpty(N_("recordset")) Then ' 用到的 Recordsets 个数
			temp = temp &  0 & " Recordsets" 
		else
			temp = temp &  N_("recordset") & " Recordsets"
		end if
		POP_MVC.dict.push base,"自动销毁",  temp
		
		' 配置项个数
		POP_MVC.dict.push base,"配置加载", POP_MVC.config.count & help( C_("HELP_CONFIG") ,"" )
		
		' session
		POP_MVC.dict.push base,"会话信息", "Session.SessionID=" & Session.SessionID & help( C_("HELP_SESSION") ,"" )
		
		' cookie 与 application
		POP_MVC.dict.push base,"Cookies",   P_("POPASP_COOKIE").Count & " Application: " & Application.Contents.Count & " Session: " & Session.Contents.Count
		
		
		
		
		' 数据库查询语句
		set sql = D_
		for each key in POP_MVC.dSql
			POP_MVC.Dict.push sql,key,POP_MVC.dSql(key)("sql") & "; -- " &  POP_MVC.dSql(key)("time") & "ms"
		next
		

		' 错误信息
		set errors = D_		
		if isObject( POP_MVC.dbTool ) Then
			if isObject( POP_MVC.dbTool.conn ) Then
				for i = 0 to POP_MVC.dbTool.conn.errors.count - 1
					POP_MVC.dict.push errors,"",POP_MVC.dbTool.conn.errors.item(i)
				next
			end if
		end if
		
		set errors = POP_MVC.dict.merge( errors , POP_MVC.dL_ )
		
		for each key in POP_MVC.dTime
			item = POP_MVC.dTime(key)
			if POP_MVC.String.reg_test( item,"^(read|write)\s+file\s+(.*?)\s+" , "i" ) then
				set matches = POP_MVC.reg.Execute(item)
				filePath = matches(0).SubMatches(1)
				item = item & readFile( filePath, "查看文件" )
			end if
			POP_MVC.dTime(key) = item
		next
		set matches = nothing
		
		
		POP_MVC.dict.push vars,"基本",base
		POP_MVC.dict.push vars,"TPL",POP_MVC.dTpl
		POP_MVC.dict.push vars,"SQL",sql
		POP_MVC.dict.push vars,"流程",POP_MVC.dFlow
		POP_MVC.dict.push vars,"性能",POP_MVC.dTime		
		POP_MVC.dict.push vars,"错误",errors
	
		
		call template.assign("trace",vars)
		
		call template.assign("runtime",G_( array("beginTime","viewEndTime") ) & "ms" ) 
		
		call template.display_1( tpl,"" )
	end sub
End Class
%>